package org.leo.dm.component.reader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import org.leo.dm.common.bean.StepResultBean;
import org.leo.dm.component.base.ICallBack;
import org.leo.dm.component.base.IReader;
import org.leo.dm.component.callback.DefaultCallback;
import org.leo.dm.component.engine.Storage;
import org.leo.dm.component.flow.Flow;
import org.leo.dm.component.plugin.columnplugin.ColumnPlugin;
import org.leo.dm.component.plugin.columnplugin.DefaultColumnPlugin;
import org.leo.dm.component.plugin.queueplugin.DefaultQueuePlugin;
import org.leo.dm.component.plugin.queueplugin.QueuePlugin;
import org.leo.dm.component.plugin.recordplugin.DefaultRecordPlugin;
import org.leo.dm.component.plugin.recordplugin.RecordPlugin;
import org.leo.dm.component.plugin.segmentplugin.DefaultSegmentPlugin;
import org.leo.dm.component.plugin.segmentplugin.SegmentPlugin;
import org.leo.dm.util.ThreadPoolUtil;
import org.leo.dm.util.ToUtil;

/**
 * 
 * Created on 2016年2月17日
 * <p> 
 * Description 读组件抽象类
 * <p> 
 * Copyright neusoft (c) 2015 .
 * 
 * @author xiao_h 
 *
 */
public abstract class Reader implements IReader, Callable<StepResultBean> {

	protected Reader() {
		// 初始化相关插件
		this.columnPlugin = new DefaultColumnPlugin();
		this.recordPlugin = new DefaultRecordPlugin();
		this.queuePlugin = new DefaultQueuePlugin();
		this.segmentPlugin = new DefaultSegmentPlugin();
		this.extendDataNames = new String[] {};
		this.callBack = new DefaultCallback();

		setThreadPoolSize(0);
		util = new ToUtil();
	}

	protected ThreadPoolUtil tpu;

	protected void setThreadPoolSize(int size) {
		if (size == 0) {
			size = 1;
		}
		tpu = new ThreadPoolUtil();
		tpu.initPool(1);
	}

	private Flow flow;

	public Flow getFlow() {
		return flow;
	}

	public void setFlow(Flow flow) {
		this.flow = flow;
	}

	protected List<Future<StepResultBean>> resultList = new ArrayList<Future<StepResultBean>>();

	private boolean runFlag = true;

	public boolean isRunFlag() {
		return runFlag;
	}

	public void setRunFlag(boolean runFlag) {
		this.runFlag = runFlag;
	}

	/**
	 * 计数器
	 */
	protected int counter;

	/**
	 * 段级计数器
	 */
	protected int seqmentcounter;

	public Map<String, Object> getDaemonData() {
		Map<String, Object> daemonData = new HashMap<String, Object>();
		util.getDuration();
		daemonData.put("countms", new Double(counter / util.getDuration()).intValue());
		daemonData.put("counter", counter);
		daemonData.put("time", util.getDuration());
		return daemonData;
	}

	protected ToUtil util;

	protected Storage storage;

	protected ICallBack callBack;

	protected StepResultBean resultBean;

	protected String[] dataNames;

	protected String[] extendDataNames;

	protected String[] excludeDataNames;

	protected ColumnPlugin columnPlugin;

	protected RecordPlugin recordPlugin;

	protected SegmentPlugin segmentPlugin;

	protected QueuePlugin queuePlugin;

	protected String[] declareDataNames;

	public void setDeclareDataNames(String[] declareDataNames) {
		this.declareDataNames = declareDataNames;
	}

	public void setSegmentPlugin(SegmentPlugin segmentPlugin) {
		this.segmentPlugin = segmentPlugin;
	}

	public void setQueuePlugin(QueuePlugin queuePlugin) {
		this.queuePlugin = queuePlugin;
	}

	public void setColumnPlugin(ColumnPlugin columnPlugin) {
		this.columnPlugin = columnPlugin;
	}

	public void setRecordPlugin(RecordPlugin recordPlugin) {
		this.recordPlugin = recordPlugin;
	}

	public void addExtendDataNames(String[] extendDataNames) {
		this.extendDataNames = extendDataNames;
	}

	public void addExcludeDataNames(String[] excludeDataNames) {
		this.excludeDataNames = excludeDataNames;
	}

	protected String[] dataValues;

	public String[] getDataNames() {
		return dataNames;
	}

	public void setDataNames(String[] dataNames) {
		this.dataNames = dataNames;
	}

	public String[] getDataValues() {
		return dataValues;
	}

	public void setDataValues(String[] dataValues) {
		this.dataValues = dataValues;
	}

	public StepResultBean getResultBean() throws Exception {
		resultBean = resultList.get(0).get();
		return resultBean;
	}

	protected void setResultBean(StepResultBean resultBean) {
		this.resultBean = resultBean;
	}

	public void setStorage(Storage storage) {
		this.storage = storage;
	}

	public void setCallBack(ICallBack callBack) {
		this.callBack = callBack;
	}

	protected int rowCount;

	public int getRowCount() {
		return rowCount;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}

	public void handle() {
		counter = 0;
		Future<StepResultBean> future = tpu.getPool().submit(this);
		resultList.add(future);
		tpu.shutdownPool();
	}
}
